package com.qf.flash.ui.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.qf.flash.R;
import com.qf.flash.http.response.BusinessInfoBean;
import com.qf.flash.utils.ImageLoader;
import com.qf.flash.widget.RatingBarView;
import com.qf.flashlib.adapter.BaseRViewItem;
import com.qf.flashlib.adapter.RViewHolder;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.permission.PermissionManager;
import com.yanzhenjie.permission.Permission;

import java.util.List;

/**
 * 创建日期：2020/3/8 on 23:17
 * 描述:
 * 作者:戴旭东
 */
public class BusinessTopItem extends BaseRViewItem<Object> {
    private Context mContext;

    public BusinessTopItem(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public boolean isItemView(Object entity, int position) {
        return entity instanceof BusinessInfoBean;
    }

    @Override
    public int getItemLayout() {
        return R.layout.itme_business_top;
    }

    @Override
    public void convert(RViewHolder holder, Object entity, int position, int itemSize) {
        BusinessInfoBean bean = (BusinessInfoBean) entity;
        ImageView ivTop = holder.getView(R.id.iv_top);
        ImageLoader.loadRoundCorners(mContext, bean.getLogo(), ivTop, 15);
        holder
                .setText(R.id.tv_name, bean.getShopName())
                .setText(R.id.tv_score, bean.getResponseScole() + "分")
                .setText(R.id.tv_address, bean.getLocation())
                .setText(R.id.tv_sub_address, bean.getLocationDesc())
                .setText(R.id.tv_name, bean.getShopName())
                .setText(R.id.tv_comment_score, bean.getResponseScole()+"")
                .setText(R.id.tv_ping, bean.getResponseCount()+"次评论")
                .setText(R.id.tv_time, "营业时间：" +bean.getOpenTime());
        RatingBarView rbs = holder.getView(R.id.rbv_star);
        rbs.setStar(Math.round(bean.getResponseScole()), false);
        RatingBarView rbv_star_comment = holder.getView(R.id.rbv_star_comment);
        rbv_star_comment.setStar(Math.round(bean.getResponseScole()), false);
        holder.getView(R.id.tv_tel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(bean.getMobile())) {
                    FlashLib.showToast("商家暂无联系电话");
                } else {
                    callPhone(bean.getMobile());
                }

            }
        });
    }

    /**
     * 拨打电话
     */
    private void callPhone(final String phone) {
        new PermissionManager(mContext).addPermission(Permission.CALL_PHONE)
                .addListener(new PermissionManager.OnPermissionListener() {
                    @Override
                    public void onSuccess(List<String> permissions) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onFailed(List<String> permissions) {

                    }
                });
    }
}
